package model

import (
	"context"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type UserBasic struct {
	ID        string `bson:"_id,omitempty"`
	Username  string `bson:"username"`
	Password  string `bson:"password"`
	CreatedAt int64  `bson:"created_at"`
	UpdatedAt int64  `bson:"updated_at"`
}

func (UserBasic) CollectionName() string {
	return "user_basic"
}

// 根据用户名查询，返回结果
func GetUserBasicByUsername(username string, password string) (*UserBasic, error) {
	ub := new(UserBasic)
	filter := bson.D{{"username", username}, {"password", password}}
	err := Mongo.Collection(UserBasic{}.CollectionName()).
		FindOne(context.Background(), filter).Decode(ub)
	if err == mongo.ErrNoDocuments {
		return nil, nil
	} else if err != nil {
		return nil, err
	}
	return ub, nil
}

// 根据用户名插入，返回结果
func InsertUserBasicByUsername(ub *UserBasic) error {
	_, err := Mongo.Collection(UserBasic{}.CollectionName()).InsertOne(context.Background(), ub)
	return err
}
